package mo.tags.greedy;

public class L376 {

    public int wiggleMaxLength(int[] nums) {
        int n = nums.length;
        if (n < 2) {
            return n;
        }
        int pre = nums[1] - nums[0];
        int ret = pre == 0 ? 1 : 2;
        for (int i = 2; i < n; i++) {
            int diff = nums[i] - nums[i-1];
            if ((diff > 0 && pre <= 0) || (diff < 0 && pre >= 0)) {
                ret++;
                pre = diff;
            }
        }
        return ret;
    }

}
